package com.mmall.admin.shiro;

import com.mmall.common.util.FilterOutUtil;
import com.mmall.common.base.ServerResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;


/**
 * 登陆校验filter
 *
 * @author gg
 * @version LoginFormFilter.java, v 0.1 2019-01-06 16:02 gg
 */
@Slf4j
public class LoginFormFilter extends FormAuthenticationFilter {

    /**
     * 跨域处理
     *
     * @param request  ServletRequest
     * @param response ServletResponse
     * @return boolean
     * @throws Exception Exception
     */
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        ShiroBaseFilter shiroBaseFilter = new ShiroBaseFilter();
        boolean result = shiroBaseFilter.preHandle(request, response);
        if (result) {
            return true;
        } else {
            return super.preHandle(request, response);
        }
    }

    /**
     * 登陆校验核心逻辑
     *
     * @param request     ServletRequest
     * @param response    ServletResponse
     * @param mappedValue Object
     * @return boolean
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        HttpServletRequest req = (HttpServletRequest) request;
        boolean result = super.isAccessAllowed(request, response, mappedValue) ||
                (!isLoginRequest(request, response) && isPermissive(mappedValue));
        if (result) {
            ShiroUser shiroUser = ShiroBaseFilter.getShiroUser(request);
            if(shiroUser == null){
                return false;
            }
            ShiroOperation.THREAD_LOCAL.set(shiroUser);
            return ShiroBaseFilter.checkShopId(req, shiroUser, response);
        }
        return false;
    }

    /**
     * 登陆校验逻辑验证失败后的处理方法，通过重写该方法取消原有的重定向
     *
     * @param request  ServletRequest
     * @param response ServletResponse
     * @return boolean
     * @throws Exception Exception
     */
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletResponse res = (HttpServletResponse) response;
        if (isLoginRequest(request, response)) {
            if (isLoginSubmission(request, response)) {
                return executeLogin(request, response);
            } else {
                return true;
            }
        } else {
            String errorStr = res.getHeader("Error");
            if (StringUtils.isNoneBlank(errorStr)) {
                res.setContentType("application/json;charset=utf-8");
                PrintWriter out = null;
                out = res.getWriter();
                out.write(errorStr);
                out.flush();
            } else {
                ServerResponse error = ServerResponse.createByErrorCodeMessage(403, "请先进行登录");
                FilterOutUtil.out(error, response);
            }
            return false;
        }
    }


}